package com.wanxi.service.Impl;

import com.wanxi.dao.Impl.ProductDaoImpl;
import com.wanxi.dao.ProductDao;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.News;
import com.wanxi.model.Product;
import com.wanxi.service.ProductService;
import com.wanxi.util.JSONCopy;
import com.wanxi.vo.ProductVO;

import java.util.Iterator;
import java.util.List;

public class ProductServiceImpl implements ProductService {
    ProductDao productDao=new ProductDaoImpl();
    @Override
    public ResultDTO findAllProduct() {
        List<Product> list=productDao.findAllProduct();
        if(list.size()>0){
            return ResultDTO.getDTO(list.size(),list);
        }
        return ResultDTO.builder().data(list).msg("no data").count(0).code(1).build();
    }

    @Override
    public ResultDTO findProductById(ProductVO productVO) {
        Product product= JSONCopy.copy(productVO,Product.class);
        Product p= productDao.findProductById(product);
        if(p!=null){
            return ResultDTO.getDTO(1,p);
        }
        return ResultDTO.builder().count(0).msg("error").code(1).build();
    }

    @Override
    public ResultDTO updateEnable(ProductVO productVO) {
        Product product= JSONCopy.copy(productVO,Product.class);
        int i= productDao.updateEnable(product);
        if(i==1){
            return ResultDTO.getDTO(i,productVO);
        }
        return ResultDTO.builder().data(productVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO addProduct(ProductVO productVO) {
        Product product= JSONCopy.copy(productVO,Product.class);
        if(product.getProductType().getId()==0){
            product.getProductType().setId(1);
        }
        int i= productDao.addProduct(product);
        if(i==1){
            return ResultDTO.getDTO(i,productVO);
        }
        return ResultDTO.builder().data(productVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO deleteProduct(ProductVO productVO) {
        Product product= JSONCopy.copy(productVO,Product.class);
        int i= productDao.deleteProduct(product);
        if(i==1){
            return ResultDTO.getDTO(i,productVO);
        }
        return ResultDTO.builder().data(productVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO updateProduct(ProductVO productVO) {
        Product product= JSONCopy.copy(productVO,Product.class);
        if(product.getProductType().getId()==0){
            product.getProductType().setId(1);
        }
        int i= productDao.updateProduct(product);
        if(i==1){
            return ResultDTO.getDTO(i,productVO);
        }
        return ResultDTO.builder().data(productVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO findProduct(ProductVO productVO) {
        Product product=JSONCopy.copy(productVO, Product.class);

        List<Product> list=productDao.findProduct(product);
        if(list.size()>0){
            Iterator<Product> iterator=list.iterator();
            while (iterator.hasNext()){
                Product p=iterator.next();
                if(p .getEnable()==0)
                    iterator.remove();
            }
            return ResultDTO.getDTO(list.size(),list);
        }
        return ResultDTO.builder().data(list).msg("no data").count(0).code(1).build();
    }

    @Override
    public ResultDTO findProductBySome(ProductVO productVO) {
        Product product=JSONCopy.copy(productVO, Product.class);
        List<Product> list=productDao.findProductBySome(product);
        if(list.size()>0){
            return ResultDTO.getDTO(list.size(),list);
        }
        return ResultDTO.builder().data(list).msg("no data").count(0).code(1).build();
    }
}
